/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class Volunteer implements DatabasePojo {

	private boolean active;
	private Card card;
	private String firstName;
	private VolunteerGroup group;
	private int id;
	private String lastName;
	private Collection<Skill> skills;

	public Volunteer() {
		this.skills = new ArrayList<Skill>();
	}

	public Volunteer(final String firstName, final String lastName, final Skill skill) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.active = true;
		this.skills = new ArrayList<Skill>();
		this.skills.add(skill);
	}

	/**
	 * @param skill
	 */
	public void addSkill(final Skill skill) {
		this.getSkills().add(skill);

	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("card", Integer.toString(this.getCard().getId()));
		element.setAttribute("firstName", this.getFirstName());
		element.setAttribute("group", Integer.toString(this.getGroup().getId()));
		element.setAttribute("lastName", this.getLastName());

		final Element skillList = element.getOwnerDocument().createElement("Skill-List");
		element.appendChild(skillList);
		for (final Skill skill : this.getSkills()) {
			final Element skillElement = element.getOwnerDocument().createElement("Skill");
			skillList.appendChild(skillElement);
			skillElement.setAttribute("skill-id", Integer.toString(skill.getId()));
		}
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Volunteer)) {
			return false;
		}
		final Volunteer other = (Volunteer) obj;
		if (this.isActive() != other.isActive()) {
			return false;
		}
		if (this.getCard() == null) {
			if (other.getCard() != null) {
				return false;
			}
		} else if (!this.getCard().equals(other.getCard())) {
			return false;
		}
		if (this.getFirstName() == null) {
			if (other.getFirstName() != null) {
				return false;
			}
		} else if (!this.getFirstName().equals(other.getFirstName())) {
			return false;
		}
		if (this.getGroup() == null) {
			if (other.getGroup() != null) {
				return false;
			}
		} else if (!this.getGroup().equals(other.getGroup())) {
			return false;
		}
		if (this.getLastName() == null) {
			if (other.getLastName() != null) {
				return false;
			}
		} else if (!this.getLastName().equals(other.getLastName())) {
			return false;
		}
		if (this.getSkills() == null) {
			if (other.getSkills() != null) {
				return false;
			}
		} else if (!this.getSkills().equals(other.getSkills())) {
			return false;
		}
		return true;
	}

	/**
	 * @return the card
	 */
	@ManyToOne(cascade = CascadeType.ALL)
	public Card getCard() {
		return this.card;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return this.firstName;
	}

	/**
	 * @return the group
	 */
	@ManyToOne(cascade = CascadeType.ALL)
	public VolunteerGroup getGroup() {
		return this.group;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return this.lastName;
	}

	public String getName() {
		return this.getFirstName() + " " + this.getLastName();
	}

	/**
	 * @return the skills
	 */
	@ManyToMany
	public Collection<Skill> getSkills() {
		return this.skills;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.isActive() ? 1231 : 1237);
		result = prime * result + (this.getCard() == null ? 0 : this.getCard().hashCode());
		result = prime * result + (this.getFirstName() == null ? 0 : this.getFirstName().hashCode());
		result = prime * result + (this.getGroup() == null ? 0 : this.getGroup().hashCode());
		result = prime * result + (this.getLastName() == null ? 0 : this.getLastName().hashCode());
		result = prime * result + (this.getSkills() == null ? 0 : this.getSkills().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param skill
	 */
	public void removeSkill(final Skill skill) {
		this.getSkills().remove(skill);
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	/**
	 * @param card
	 *            the card to set
	 */
	public void setCard(final Card card) {
		this.card = card;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(final String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @param group
	 *            the group to set
	 */
	public void setGroup(final VolunteerGroup group) {
		this.group = group;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param lastName
	 *            the lastName to set
	 */
	public void setLastName(final String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @param skills
	 *            the skills to set
	 */
	public void setSkills(final Collection<Skill> skills) {
		this.skills = skills;
	}

	@Override
	public String toString() {
		return this.getFirstName() + " " + this.getLastName();
	}

}
